/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;
import data.UserData;
import data.IdentitasData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author Muhammad Dony
 */
public class IdentitasDB extends ConnectDB {
    private String sql;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    
    private List getIdentitasData(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listdata2 = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        IdentitasData listIdentitas;
        
        while(rs.next()){
            listIdentitas = new IdentitasData();
            listIdentitas.setID(rs.getString(1));
            listIdentitas.setNama1(rs.getString(2));
            listIdentitas.setNama2(rs.getString(3));
            listIdentitas.setTempat(rs.getString(4));
            listIdentitas.setTanggal(rs.getString(5)); 
            listIdentitas.setBulan(rs.getString(6));
            listIdentitas.setTahun(rs.getString(7));
            listIdentitas.setAlamat(rs.getString(8));
            listIdentitas.setHp(rs.getString(9));
            listIdentitas.setEmail(rs.getString(10));
            listIdentitas.setJurusan(rs.getString(11));
            listIdentitas.setFakultas(rs.getString(12));
            listIdentitas.setUniversitas(rs.getString(13));
            listIdentitas.setAngkatan(rs.getString(14));
            listIdentitas.setTinggi(rs.getString(15));
            listIdentitas.setBerat(rs.getString(16));
            listIdentitas.setDarah(rs.getString(17));
            listIdentitas.setMotto(rs.getString(18));
            listdata2.add(listIdentitas);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listdata2;
    }
    
    private List getIdentitas2(String sql) throws SQLException, ClassNotFoundException{
        // mempersiapkan list dataPelaporan
        List listdata3 = new ArrayList();
        //membuat koneksi , createConnection terdapat dalam DBClass
        con = createConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);
        // mempersiapkan object dataPelaporan
        IdentitasData listIdentitas;
        
        while(rs.next()){
            listIdentitas = new IdentitasData();
            listIdentitas.setID(rs.getString(1));
            listIdentitas.setNama1(rs.getString(2));
            listIdentitas.setUniversitas(rs.getString(3));
            listIdentitas.setAngkatan(rs.getString(4));
            listdata3.add(listIdentitas);
        }
        // menutup semua koneksi db
        rs.close();
        stmt.close();
        con.close();
        //return value list data pelaporan
        return listdata3;
    }
    
    public List getIdentitasById(String ID) throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `identitas` WHERE `ID`='"+ID+"';";
        return getIdentitasData(sql);
    }
    public List getIdentitasdata() throws SQLException, ClassNotFoundException {
        sql = "SELECT * from `identitas` ;";
        return getIdentitasData(sql);
    }
    
    public List getIdentitassingkat() throws SQLException, ClassNotFoundException {
        sql = "SELECT `ID`,`nama1`,`universitas`,`angkatan` FROM `identitas`;";
        return getIdentitas2(sql);
    }
    
    
    
    
    
    private void actidentitas(String sql) throws ClassNotFoundException, SQLException{
        // membuat koneksi
        con = createConnection();
        stmt = con.createStatement();
        // execute query
        stmt.execute(sql);
        // menutup koneksi db
        stmt.close();
        con.close();
    }
    
    public void updateIdentitas(IdentitasData listIdentitas) throws ClassNotFoundException, SQLException{
        // query
        sql = "UPDATE `identitas` SET `nama1`='"+listIdentitas.getNama1()+
                                    "',`nama2`='"+listIdentitas.getNama2()+
                                    "',`tempat`='"+listIdentitas.getTempat()+
                                    "',`tanggal`='"+listIdentitas.getTanggal()+
                                    "',`bulan`='"+listIdentitas.getBulan()+
                                    "',`tahun`='"+listIdentitas.getTahun()+
                                    "',`alamat`='"+listIdentitas.getAlamat()+
                                    "',`hp`='"+listIdentitas.getHp()+
                                    "',`email`='"+listIdentitas.getEmail()+
                                    "',`jurusan`='"+listIdentitas.getJurusan()+
                                    "',`fakultas`='"+listIdentitas.getFakultas()+
                                    "',`universitas`='"+listIdentitas.getUniversitas()+
                                    "',`angkatan`='"+listIdentitas.getAngkatan()+
                                    "',`tinggi`='"+listIdentitas.getTinggi()+
                                    "',`berat`='"+listIdentitas.getBerat()+
                                    "',`darah`='"+listIdentitas.getDarah()+
                                    "',`motto`='"+listIdentitas.getMotto()+
                                    "' WHERE `ID`='"+listIdentitas.getID()+"';";
        // memanggil method untuk execute query
        actidentitas(sql);
    }
    
}
